package com.zw.gulimall.order.test;

import com.zw.common.entity.order.OrderEntity;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order/mq")
public class MqTestController {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/createExchange")
    public String createExchange() {
        /**
         * String name   交换机名称,
         * boolean durable, 是否持久化
         * boolean autoDelete, 是否自动删除
         * Map<String, Object> arguments  参数
         */
        Exchange exchange = new DirectExchange("hello-java-exchange", true, false, null);
        amqpAdmin.declareExchange(exchange);
        return "创建交换机成功" + exchange.getName();
    }

    @GetMapping("/createQueue")
    public String createQueue() {
        /**
         * String name, 队列名称
         * boolean durable,  是否持久化
         * boolean exclusive, 是否独占
         * boolean autoDelete, 是否自动删除
         * @Nullable Map<String, Object> arguments 参数
         */
        Queue queue = new Queue("hello-java-equeque", true, false, false, null);
        amqpAdmin.declareQueue(queue);
        return "创建队列成功" + queue.getName();
    }

    @GetMapping("/declareBinding")
    public String declareBinding() {
        /**
         * String destination,目的地name，可以时交换机也可以时队列
         * DestinationType destinationType, 目的地类型【queue或exchange（路由）】
         * String exchange,待绑定交换机
         * String routingKey,路由键
         * @Nullable Map<String, Object> arguments 参数
         */
        Binding binding = new Binding("hello-java-equeque", Binding.DestinationType.QUEUE,
                "hello-java-exchange", "hello.java", null);
        amqpAdmin.declareBinding(binding);
        return "绑定成功" + binding.getDestination() + "" + binding.getExchange() + "" + binding.getDestinationType() + "" + binding.getRoutingKey();
    }


    @GetMapping("/sendMsg")
    public String sendMsg() {
        for (int i = 0; i < 10; i++) {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setAutoConfirmDay(111);
            orderEntity.setOrderSn("string");
            rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", orderEntity);
        }
        return "消息发送成功";
    }
}
